package com.zhangtai.utils.Excel;


import com.zhangtai.utils.DateUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

/**
 * 实体对象工具类
 * @author gaof
 * @date 2017年3月29日
 */
public class EntityUtil {

	/** 
     * 根据属性名获取属性值 
     * */  
    public static Object getFieldValueByName(String fieldName, Object o) {  
        try {  
            String firstLetter = fieldName.substring(0, 1).toUpperCase();  
            String getter = "get" + firstLetter + fieldName.substring(1);  
            Method method = o.getClass().getMethod(getter, new Class[] {});  
            Object value = method.invoke(o, new Object[] {});  
            return value;  
        } catch (Exception e) {  
            // log.error(e.getMessage(), e);  
            return null;  
        }  
    }  
  
    /** 
     * 类名(obj_name)获取属性类型(f_type)，属性名(f_name)，属性值(f_value)的map组成的list 
     * */  
    @SuppressWarnings("unused")  
    public static List getFiledsInfo(Object o) {  
  
        String obj_name = o.getClass().getSimpleName().toString();  
        Field[] fields = o.getClass().getDeclaredFields();  
        String[] fieldNames = new String[fields.length];  
        List<Map> list = new ArrayList();  
        Map<String, Object> infoMap;  
  
        for (int i = 0; i < fields.length; i++) {  
            infoMap = new HashMap<String, Object>();  
  
            infoMap.put("obj_name", obj_name);  
            infoMap.put("f_type", fields[i].getType().toString());  
            infoMap.put("f_name", fields[i].getName());  
            infoMap.put("f_value", getFieldValueByName(fields[i].getName(), o));  
            list.add(infoMap);  
        }  
        
        return list;  
    }

    /**
     * MAP列表转对象列表
     * @param maplist
     * @param clazz
     * @return
     */
    public static List ListMapToListObj(List<Map<String, Object>> maplist, Class clazz) {
        try {
            List list = new ArrayList();
            for(Map<String, Object> map: maplist) {
                Object obj = clazz.newInstance();
                Field[] fs = obj.getClass().getDeclaredFields();
                for(Field field: fs) {
                    String name = field.getName();
                    //设置些属性是可以访问的
                    field.setAccessible(true);
                    Type type = field.getGenericType();
                    for(Map.Entry<String, Object> entry: map.entrySet()) {
                        if(entry.getValue() == null) {
                            continue;
                        }
                        if(entry.getKey().equalsIgnoreCase(name)) {
                            try {
                                if(type.getTypeName().equals("java.lang.Long")) {
                                    Object o = entry.getValue();
                                    if(o instanceof Long) {
                                        field.set(obj, entry.getValue());
                                    }
                                    else if(o instanceof Integer) {
                                        field.set(obj, new Long((Integer) entry.getValue()));
                                    }
                                    else if(o instanceof BigDecimal) {
                                        field.set(obj, ((BigDecimal)entry.getValue()).longValue());
                                    }
                                    else if(o instanceof BigInteger) {
                                        field.set(obj, ((BigInteger)entry.getValue()).longValue());
                                    }
                                }
                                else if(type.getTypeName().equals("java.lang.Integer")) {
                                    Object o = entry.getValue();
                                    if(o instanceof Integer) {
                                        field.set(obj, entry.getValue());
                                    }
                                    else if(o instanceof BigDecimal) {
                                        field.set(obj, ((BigDecimal)entry.getValue()).intValue());
                                    }
                                    else if(o instanceof BigInteger) {
                                        field.set(obj, ((BigInteger)entry.getValue()).intValue());
                                    }
                                    else if(o instanceof String) {
                                        field.set(obj, Integer.parseInt(entry.getValue().toString()));
                                    }
                                }
                                else if(type.getTypeName().equals("java.util.Date")) {
                                    Object o = entry.getValue();
                                    if(o instanceof Integer) {
                                        field.set(obj, new Date(Integer.parseInt(o.toString())));
                                    }
                                    else if(o instanceof Long) {
                                        field.set(obj, new Date(Long.parseLong(o.toString())));
                                    }
                                    else if(o instanceof BigDecimal) {
                                        field.set(obj, new Date(((BigDecimal)entry.getValue()).longValue()));
                                    }
                                    else if(o instanceof BigInteger) {
                                        field.set(obj, new Date(((BigInteger)entry.getValue()).longValue()));
                                    }
                                    else if(o instanceof String) {
                                        String s = entry.getValue().toString();
                                        if(s != null && s.length() == 10)
                                            field.set(obj, DateUtils.parseDateYMD(s));
                                        else
                                            field.set(obj, DateUtils.parseDateYMDHMSS(s));
                                    }else if(o instanceof Date) {
                                        field.set(obj, o);
                                    }
                                }else if(type.getTypeName().equals("class java.math.BigDecimal")) {
                                    field.set(obj, (BigDecimal)entry.getValue());
                                }
                                else {
                                    field.set(obj, entry.getValue());
                                }
                            } catch (IllegalArgumentException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            break;
                        }
                    }
                }
                list.add(obj);
            }
            return list;
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 列表对象转列表对象
     * @param list1
     * @param clazz
     * @return
     */
    public static List ListObjToListObj(List list1, Class clazz) {
        try {
            List list = new ArrayList();
            for(Object obj1: list1) {
                Object obj2 = clazz.newInstance();
                Field[] fs = obj2.getClass().getDeclaredFields();
                for(Field f: fs) {
                    String name = f.getName();
                    //设置些属性是可以访问的
                    f.setAccessible(true);
                    Field[] fields1 = obj1.getClass().getDeclaredFields();
                    for(Field field: fields1) {
                        field.setAccessible(true);
                        String oName = field.getName();
                        if(name.equals(oName)) {
                            f.set(obj2, field.get(obj1)) ;       //给属性设值
                            break;
                        }
                    }
                }
                list.add(obj2);
            }
            return list;
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
